﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class StockInfo : System.Web.UI.Page
{
    private static string sqlconn = System.Configuration.ConfigurationManager.AppSettings["strConn"].ToString().Trim();
    protected void Page_Load(object sender, EventArgs e)
    {
        /*if (!IsPostBack)
        {
            if (Session["adminFlag"] == null)
            {
                Response.Write("<script>top.location.href='/login.aspx';</script>");
                return;
            }
        }*/
        if (!IsPostBack)
        {
            BindYear();
            BindMonth();
            BindDay();

        }
    }
    protected void GoodNo_TextChanged(object sender, EventArgs e)
    {

    }
    protected void Btn_Add_Click(object sender, EventArgs e)
    {
       
       
        string id = txtStockID.Text.Trim();
        string id1 = txtMerch.Text.Trim();
        string name = txtMerch1.Text.Trim();
        string desc = txtMerch2.Text.Trim();
        string provideid = ProvideID.SelectedItem.Text.Trim();
        string price = txtPrice.Text.Trim();
        string num = txtNumber.Text.Trim();
        string total = txtTotalPrice.Text.Trim();
        string year = DropDownList1.SelectedItem.Text.Trim();
        string month = DropDownList2.SelectedItem.Text.Trim();
        string day = DropDownList3.SelectedItem.Text.Trim();
        if (month.Length == 1)
        {
            month = "0" + month;
        }
        string buydate = year + month + day;
        users us = new users();
        us.name = name;
        us.id = id;
        us.id1=id1 ;
        us.price = price;
        us.total =total;
        us.num = num;
        us.buydate = buydate;
        int result = us.isExist(us);
        SqlConnection conn = new SqlConnection(sqlconn);
        try
        {
            conn.Open();
            string sql = "insert into StockInfo (StockID,MerchID,MerchName,MerchDescn,ProvideID,MerchPrice,MerchNum,MerchSumP,StockDate) VALUES ('" + id + "','" + id1 + "','" + name + "','" + desc + "','" + provideid + "','" + price + "','" + num + "','" + total + "','" + buydate + "')";
            SqlCommand comm = new SqlCommand(sql, conn);
            comm.ExecuteNonQuery();
            Response.Write("<script>alert(\"商品进货信息登记成功!\")</script>");
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
        
    }
   
    protected void GVmerch_SelectedIndexChanged(object sender, EventArgs e)
    {
        float price = Convert.ToSingle(this.txtPrice.Text);
        int number = Convert.ToInt32(this.txtNumber.Text);
        float totalPrice = price * number;
        this.txtTotalPrice.Text = totalPrice.ToString();
    }
    protected void txtPrice_TextChanged(object sender, EventArgs e)
    {
        float price = Convert.ToSingle(this.txtPrice.Text);
        int number = Convert.ToInt32(this.txtNumber.Text);
        float totalPrice = price * number;
        this.txtTotalPrice.Text = totalPrice.ToString();
    }
    protected void txtNumber_TextChanged(object sender, EventArgs e)
    {
        float price = Convert.ToSingle(this.txtPrice.Text);
        int number = Convert.ToInt32(this.txtNumber.Text);
        float totalPrice = price * number; 
        this.txtTotalPrice.Text = totalPrice.ToString();
    }
    protected void ProvideID_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    void BindYear()
    {
        DropDownList1.Items.Clear();
        int StarYear = DateTime.Now.Year - 30;
        int EndYear = DateTime.Now.Year;
        for (int i = StarYear; i <= EndYear; i++)
        {
            DropDownList1.Items.Add(new ListItem(i.ToString()));
        }
        DropDownList1.SelectedValue = EndYear.ToString();
    }

    void BindMonth()
    {
        DropDownList2.Items.Clear();
        for (int i = 1; i <= 12; i++)
        {
            DropDownList2.Items.Add(new ListItem(i.ToString()));
        }
    }
    void BindDay()
    {
        DropDownList3.Items.Clear();
        string Year = DropDownList1.SelectedValue;
        string Month = DropDownList2.SelectedValue;
        int day = DateTime.DaysInMonth(int.Parse(Year), int.Parse(Month));
        for (int i = 1; i < day; i++)
        {
            DropDownList3.Items.Add(new ListItem(i.ToString()));
        }
    }
}